<template>
	<a-card :bordered="false" style="margin-bottom: 10px">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="标题" name="title">
						<a-input v-model:value="searchFormState.title" placeholder="请输入标题" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="类型" name="type">
						<a-select
							v-model:value="searchFormState.type"
							placeholder="请选择类型"
							:options="typeOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="发布时间" name="publicTime">
						<a-range-picker v-model:value="searchFormState.publicTime" value-format="YYYY-MM-DD HH:mm:ss" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="状态" name="status">
						<a-select
							v-model:value="searchFormState.status"
							placeholder="请选择状态"
							:options="statusOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			@resizeColumn="handleResizeColumn"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('noticeInfoAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('noticeInfoBatchDelete')"
						buttonName = "删除"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchNoticeInfo"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'type'">
					{{ $TOOL.dictTypeData('notice_type', record.type) }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					{{ $TOOL.dictTypeData('notice_status', record.status) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formViewRef.onOpen(record)">查看详情</a>
						<a-divider type="vertical" v-if="hasPerm(['noticeInfoEdit', 'noticeInfoDelete'], 'and')" />
						<a @click="updStatus(record,1)" v-if="hasPerm('noticeInfoEdit') && record.status==='0'">发布</a>
						<a @click="updStatus(record,0)" v-if="hasPerm('noticeInfoEdit') && record.status==='1'">撤回</a>
						<a-divider type="vertical" v-if="hasPerm(['noticeInfoEdit', 'noticeInfoDelete'], 'and') && record.status==='0'" />
						<a @click="formRef.onOpen(record)" v-if="hasPerm('noticeInfoEdit') && record.status==='0'">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['noticeInfoEdit', 'noticeInfoDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteNoticeInfo(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('noticeInfoDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<FormView ref="formViewRef"/>
</template>

<script setup name="noticeinfo">
	import Form from './form.vue'
	import FormView from './formview.vue'
	import noticeInfoApi from '@/api/biz/notice/noticeInfoApi'
	import {message} from "ant-design-vue";
	import tool from "@/utils/tool";

	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const formViewRef = ref()
	const typeOptions = tool.dictList('notice_type')
	const statusOptions = tool.dictList('notice_status')

	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '类型',
			dataIndex: 'type',
			ellipsis: true,
			resizable: true,
			width: 100,
			minWidth: 100,
			maxWidth: 300,
			align: 'center'
		},
		{
			title: '标题',
			dataIndex: 'title',
			ellipsis: true,
			resizable: true,
			width: 100,
			minWidth: 100,
			maxWidth: 300,
			align: 'center'
		},
		{
			title: '发布人姓名',
			dataIndex: 'publicUserName',
			ellipsis: true,
			resizable: true,
			width: 100,
			minWidth: 100,
			maxWidth: 300,
			align: 'center'
		},
		{
			title: '发布时间',
			dataIndex: 'publicTime',
			ellipsis: true,
			resizable: true,
			width: 100,
			minWidth: 100,
			maxWidth: 300,
			align: 'center'
		},
		{
			title: '状态',
			dataIndex: 'status',
			ellipsis: true,
			resizable: true,
			width: 100,
			minWidth: 100,
			maxWidth: 300,
			align: 'center'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['noticeInfoEdit', 'noticeInfoDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '300px'
		})
	}
	const handleResizeColumn =(w, col) => {
		col.width = w
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// publicTime范围查询条件重载
		if (searchFormParam.publicTime) {
			searchFormParam.startPublicTime = searchFormParam.publicTime[0]
			searchFormParam.endPublicTime = searchFormParam.publicTime[1]
			delete searchFormParam.publicTime
		}
		return noticeInfoApi.noticeInfoPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}

	// 修改状态
	const updStatus = (record,status) => {
		let params ={
			id: record.id,
			status: status
		}
		noticeInfoApi.noticeInfoUpdStatus(params).then(() => {
			message.success('修改成功！')
			table.value.refresh(true)
		})
	}

	// 删除
	const deleteNoticeInfo = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		noticeInfoApi.noticeInfoDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchNoticeInfo = (params) => {
		noticeInfoApi.noticeInfoDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>
